package com.intelligent.health.setmeal.controller;

import com.intelligent.health.setmeal.dto.CheckItemDto;
import com.intelligent.health.setmeal.service.CheckItemService;
import com.intelligent.health.setmeal.pojo.CheckItem;
import com.intelligent.health.setmeal.vo.CheckItemVo;
import entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/13/22:35
 * @Description: 检查项
 */
@Slf4j
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 分页查询 checkitem
     *
     * @param checkItemDto 查询条件
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return 检查项集合
     */
    @PostMapping("/all/{pageNum}/{pageSize}")
    public R queryCheckItemsByPage(@RequestBody CheckItemDto checkItemDto,
                                   @PathVariable("pageNum") Integer pageNum,
                                   @PathVariable("pageSize") Integer pageSize) {
        PageResult<CheckItemVo> checkItems = checkItemService.queryCheckItemByPage(checkItemDto, pageNum, pageSize);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkItems",checkItems);
    }

    /**
     * 根据checkItemId找到checkItem
     *
     * @param checkItemId 检查项id
     * @return 检查项信息
     */
    @GetMapping("/{checkItemId}")
    public R queryCheckItemById(@PathVariable("checkItemId") Integer checkItemId) {
        CheckItem checkItem = checkItemService.queryCheckItemById(checkItemId);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkItem",checkItem);
    }

    /**
     * 添加checkItem
     *
     * @param checkItem 检查项信息
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKITEM_ADD')")
    @PostMapping("/add")
    public R addCheckItem(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.addCheckItem(checkItem);
        } catch (Exception e) {
            log.error("添加检查项失败: {}", checkItem);
            return R.setResult(ResultCodeEnum.INSERT_ERROR);
        }
        log.info("添加检查项成功: {}", checkItem);
        return R.setResult(ResultCodeEnum.INSERT_SUCCESS);
    }

    /**
     * 编辑checkItem
     *
     * @param checkItem 检查项信息
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKITEM_EDIT')")
    @PutMapping("/edit")
    public R editCheckItem(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.editCheckItem(checkItem);
        } catch (Exception e) {
            log.error("编辑检查项失败: {}", checkItem);
            return R.setResult(ResultCodeEnum.UPDATE_ERROR);
        }
        log.info("编辑检查项成功: {}", checkItem);
        return R.setResult(ResultCodeEnum.UPDATE_SUCCESS);
    }

    /**
     * 逻辑删除checkItem 相当于禁用
     *
     * @param checkItemId 检查项id
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKITEM_DELETE')")
    @DeleteMapping("/del/{checkItemId}")
    public R delCheckItem(@PathVariable("checkItemId") Integer checkItemId) {
        try {
            checkItemService.delCheckItemById(checkItemId);
        } catch (Exception e) {
            log.error("删除检查项失败: {}", checkItemId);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("删除检查项成功: {}", checkItemId);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 批量删除检查项
     *
     * @param checkItemIds 检查项ids
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('CHECKITEM_DELETE')")
    @DeleteMapping("/delMore")
    public R delMoreCheckItem(@RequestBody ArrayList<Integer> checkItemIds) {

        try {
            checkItemService.delBatchCheckItem(checkItemIds);
        } catch (Exception e) {
            log.error("批量删除检查项失败: {}", checkItemIds);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("批量删除检查项成功: {}", checkItemIds);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 查询检查项数据 for export Excel
     *
     * @param hashMap key: checkItemDto value: CheckItemDto(必须)
     *                key: pageInfo value: PageInfo(非必须)
     * @return 检查项集合
     */
    @PostMapping("/all/excel")
    public R queryCheckItems(@RequestBody HashMap<String, Object> hashMap) {
        if (hashMap.get("checkItemDto") == null) {
            return R.setResult(ResultCodeEnum.QUERY_ERROR);
        }
        List<CheckItem> checkItems = checkItemService.queryCheckItems(hashMap);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkItems",checkItems);
    }
}
